"use strict";
const assert = require("assert");

const signature = require("./lib/signature");
const cfg = require("./config/server");
const tool = require("./lib/common");

const defaultKey = cfg.key;
const defaultSecret = cfg.secret;
const defaultServer = cfg.aqServer.url;
const defaultVersion = cfg.aqServer.version;

const defaultBit = 4;
const defaultSurvialSeconds = 90;
const defaultFrozenSeconds = 10;
const getParamToGenerateHeader = function(token, time, key) {
	return {
		token,
		time,
		key
	};
};
const initSecure = function(key, secret, aqServer, version) {
	key = key || defaultKey;
	secret = secret || defaultSecret;
	aqServer = tool.removeTailSlash(aqServer) || defaultServer;
	version = version || defaultVersion;
	const getCaptchaUrl = queryString => `${aqServer}/${version}/secure-captchas?${queryString}`;

	const getSmsCode = function(param){
		assert.ok(param, "param can't be null;");
		assert.ok(param.addresser, "addresser can't be null;");
		assert.ok(param.recipient, "recipient can't be null;");
		const time = Date.now();
		const rawSignature = `${secret}&${time}&/${version}/secure-codes`;
		const getSignature = () => signature.encrypt(rawSignature);
		const getRequestOptions = (signature) => {
			const rawHeader = getParamToGenerateHeader(signature, time, key);
			return {
				json: true,
				method: "POST",
				url: `${aqServer}/${version}/secure-codes`,
				headers: tool.getHeaders(rawHeader),
				body: {
					bits: param.bits || defaultBit, //注意这个长度是sms code的长度，跟上面的captcha的长度是两回事
					survival_seconds: param.survival_seconds || defaultSurvialSeconds, //同理，这个生存时间是sms code的生存时间
					frozen_seconds: param.frozenSeconds || defaultFrozenSeconds, //冻结时间，在冻结时间内请求接口的话，会被忽略
					type: param.type || "phone",
					addresser: param.addresser,
					recipient: param.recipient,
					template: param.template,
					captcha: param.captcha
				}
			};
		};
		const doGetSms = function(options) {
			console.log("获取短信验证码的options是", options);
			return tool.sendRequest(options);
		};
		return getSignature().then(getRequestOptions).then(doGetSms);
	};

	const validateSmsCode = function(param){
		assert.ok(param, "param can't be null;");
		assert.ok(param.code, "code can't be null;");
		assert.ok(param.addresser, "addresser can't be null;");
		assert.ok(param.recipient, "recipient can't be null;");
		const time = Date.now();
		const rawSignature = `${secret}&${time}&/${version}/secure-codes/verify`;
		const getSignature = () => signature.encrypt(rawSignature);
		const getRequestOptions = (signature) => {
			const rawHeader = getParamToGenerateHeader(signature, time, key);
			return {
				json: true,
				method: "POST",
				url: `${aqServer}/${version}/secure-codes/verify`,
				headers: tool.getHeaders(rawHeader),
				body: {
					addresser: param.addresser,
					recipient: param.recipient,
					code: param.code
				}
			};
		};
		const doValidateSms = function(options) {
			console.log("校验短信验证码的options是", options);
			return tool.sendRequest(options);
		};
		return getSignature().then(getRequestOptions).then(doValidateSms);
	};

	const validateCaptcha = function(param){
		assert.ok(param, "param can't be null;");
		assert.ok(param.addresser, "addresser can't be null;");
		assert.ok(param.recipient, "recipient can't be null;");
		assert.ok(param.captcha, "captcha can't be null;");
		const time = Date.now();
		const rawSignature = `${secret}&${time}&/${version}/secure-captchas`;
		const getSignature = () => signature.encrypt(rawSignature);
		const getRequestOptions = (signature) => {
			const rawHeader = getParamToGenerateHeader(signature, time, key);
			return {
				json: true,
				method: "POST",
				url: `${aqServer}/${version}/secure-captchas`,
				headers: tool.getHeaders(rawHeader),
				body: {
					addresser: param.addresser,
					recipient: param.recipient,
					captcha: param.captcha
				}
			};
		};
		const doValidateCaptcha = function(options) {
			console.log("验证图片码的options是", options);
			return tool.sendRequest(options);
		};
		return getSignature().then(getRequestOptions).then(doValidateCaptcha);
	};

	return {
		getCaptchaUrl,
		getSmsCode,
		validateSmsCode,
		validateCaptcha
	};
};

module.exports = initSecure;